'''
Created on Aug 25, 2011

@author: david
'''

from app.GHT.actions import GHTAction

class Upload(GHTAction):
    """
    Default Action 
    spawns index.gen
    """
    def execute(self):
        req = self.request
        
        from pspy.gae.bulkloader import BulkLoader
        from cStringIO import StringIO
        import json
        
        #get parameters
        fl,sz = req.getFileData('import')
        
        name = req.getParameter('model')
        
        #empty object
        status = {}        
        
        #run csv import
        bulk = BulkLoader(StringIO(fl), "app.GHT.model.db.%s" % name)
        
        try:
            bulk.run()
            status["file_status"] = "100::File Imported Successfully: "
            status["row_count"] = len(fl.split('\n'))
        except Exception, ex:
            status["file_status"] = "500::Failed Importing: "+ex
        
        return self.JSONOutput(json.dumps(status))
    
    def sess_check(self):
        return True